package org.seafloor.common.dao.Impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.SessionFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.seafloor.common.dao.BaseDao;
import org.seafloor.system.model.UserVO;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.orm.hibernate3.HibernateTemplate;

public class BaseDaoImpl<E> implements BaseDao<E>{
	protected HibernateTemplate hibernateTemplate;

	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}
	
	public void delete(E e) throws Exception {
		try {
			this.hibernateTemplate.delete(e);
		} catch (DataIntegrityViolationException e1) {
			throw new DataIntegrityViolationException("该信息已被引用，请先删除引用");
		}
	}

	public void save(E e) throws Exception {
		try{
		this.hibernateTemplate.save(e);
		}catch (Exception e1) {
			throw new Exception(e1.getMessage());
		}
	}

	public void update(E e) throws Exception {
		try {
			this.hibernateTemplate.update(e);
		} catch (Exception e1) {
			throw new Exception("修改出错");
		}

	}
	
	/**
	 * 查询表中所有的数据
	 */
	@SuppressWarnings("unchecked")
	public List<E> queryAllData(E e) throws Exception {
		DetachedCriteria detachedCriteria = DetachedCriteria.forClass(e
				.getClass());
		List<E> list = new ArrayList<E>();
		try {
			list = this.hibernateTemplate.findByCriteria(detachedCriteria);
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		return list;
	}
}
